package com.my.easyrules.db;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.my.easyrules.db.enums.CompositeRuleTypeEnum;
import com.my.easyrules.db.helper.BizRuleHelper;
import com.my.easyrules.db.mysql.domain.BizRuleComposePo;
import com.my.easyrules.db.mysql.domain.BizRulePo;
import com.my.easyrules.db.mysql.service.BizRuleComposePoService;
import com.my.easyrules.db.mysql.service.BizRulePoService;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.support.RuleDefinition;
import org.jeasy.rules.support.composite.ActivationRuleGroup;
import org.jeasy.rules.support.composite.CompositeRule;
import org.jeasy.rules.support.composite.ConditionalRuleGroup;
import org.jeasy.rules.support.composite.UnitRuleGroup;
import org.mvel2.ParserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: Mr.xiu
 * @Description: 获取规则rules集合
 * @Date: 2021/7/28 16:24
 */
@Component
public class BizRulesBuild {
    @Autowired
    private BizRulePoService bizRulePoService;
    @Autowired
    private BizRuleComposePoService bizRuleComposePoService;

    /**
     * 构建rules配置
     */
    public Rules fetchBuildRules(String ruleName) {
        CompositeRule compositeRule = ruleDefinitions(ruleName);
        Rules rules = new Rules();
        rules.register(compositeRule);
        return rules;
    }

    /**
     * 找出该组所有的规则
     *
     * @param groupCode 规则组
     * @return 结果
     */
    public CompositeRule ruleDefinitions(String groupCode) {
        //规则
//        List<BizRulePo> bizRuleList =
        LambdaQueryChainWrapper<BizRulePo> eq = bizRulePoService.lambdaQuery()
                .eq(BizRulePo::getName, groupCode);

//        QueryWrapper<BizRulePo> wrapper=new QueryWrapper<>();
//        wrapper.eq("NAME", groupCode);
        LambdaQueryWrapper<BizRulePo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BizRulePo::getName, groupCode);
        List<BizRulePo> bizRuleList = bizRulePoService.list(wrapper);
        if (CollectionUtils.isEmpty(bizRuleList)) {
//          throw new BusinessException("规则定义不存在");
        }
        CompositeRule compositeRule;
        BizRulePo bizRule = bizRuleList.get(0);
        switch (CompositeRuleTypeEnum.of(bizRule.getCompositeType())) {
            case AND:
                compositeRule = new UnitRuleGroup(bizRule.getName());
                break;
            case ALL:
                compositeRule = new ConditionalRuleGroup(bizRule.getName());
                break;
            default:
                compositeRule = new ActivationRuleGroup(bizRule.getName());
        }
        compositeRule.setDescription(bizRule.getDescription());
        compositeRule.setPriority(bizRule.getPriority());


        //规则组合数据
        LambdaQueryWrapper<BizRuleComposePo> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(BizRuleComposePo::getRuleId, bizRule.getId());
        List<BizRuleComposePo> bizRuleComposeList = bizRuleComposePoService.list(wrapper2);
//        List<BizRuleComposePo> bizRuleComposeList =
//                bizRuleComposePoService.selectByRule(bizRule.getId());
        CompositeRule finalCompositeRule = compositeRule;
        BizRuleHelper bizRuleHelper = new BizRuleHelper(new ParserContext());
        //规则定义
        bizRuleComposeList.forEach(bizRuleCompose -> {
            RuleDefinition ruleComposeDefinition = new RuleDefinition();
            ruleComposeDefinition.setName(bizRuleCompose.getName());
            try {
                BeanUtil.copyProperties(bizRuleCompose,ruleComposeDefinition);
                //condition数据库保留字段 这里替换
                ruleComposeDefinition.setCondition(bizRuleCompose.getConditions());
//                BeanCopyUtil.getBean(bizRuleCompose, ruleComposeDefinition);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (!StringUtils.isEmpty(bizRuleCompose.getActions())) {
                List<String> actions = Arrays.asList(bizRuleCompose.getActions().split(";"));
                ruleComposeDefinition.setActions(actions);
            }
            Rule rule = bizRuleHelper.createSimpleRule(ruleComposeDefinition);
            finalCompositeRule.addRule(rule);
        });
        return finalCompositeRule;
    }
}
